﻿using System.Collections.Generic;
using System.Linq;
using Missbot.AIModule.Base;
using Missbot.Core.Base;

namespace Missbot.AIModule.Gaming.SignIn
{
    public abstract class SignInBaseMod : IAIModule
    {
        public override string Name => "签到";

        public AIModuleMgr AIModuleMgr { get; set; }

        private readonly Dictionary<string, SignInGroupRecord> GroupSignInDic;

        protected SignInBaseMod()
        {
            var records = SignInGroupDA.Get();
            GroupSignInDic = records.ToDictionary(p => p.GroupID, p => p);
        }

        [ModuleEnter(Command = "设置签到内容", Description = "设置签到内容(不能与系统自带命令重复)", AuthLvl = AuthorityLevel.Owner, SyntaxHint = "[签到内容]")]
        public bool SetSignContent(InboxSession session, string content)
        {
            if (content != "签到" && AIModuleMgr.AllModules.SelectMany(p => p.EnterUnits).Any(comm => comm.Attr.Command == content))
            {
                session.SendBack("不能与系统自带命令重复！");
                return false;
            }

            if (GroupSignInDic.ContainsKey(session.Group.ID))
            {
                var groupSignIn = GroupSignInDic[session.Group.ID];
                groupSignIn.Content = content;
                groupSignIn.Update();
            }
            else
            {
                var groupSignIn = new SignInGroupRecord { GroupID = session.Group.ID, Content = content };
                groupSignIn.Insert();
                GroupSignInDic.Add(session.Group.ID, groupSignIn);
            }

            session.SendBack("设置成功！");
            return true;
        }

        protected override bool OnInboxImpl(InboxSession session)
        {
            if (base.OnInboxImpl(session))
            {
                return true;
            }

            if (session.Group?.ID == null)
            {
                return false;
            }

            // 群组签到验证
            var groupInfo = GroupSignInDic.ContainsKey(session.Group.ID) ? GroupSignInDic[session.Group.ID] : null;
            var signInContent = groupInfo == null ? "签到" : groupInfo.Content;

            if (session.Text != signInContent)
            {
                if (session.Text == "签到")
                {
                    session.SendBack("请使用 今日签到内容 命令获取今天的签到口令！");
                }

                return false;
            }

            // 个人签到验证
            if (SignInSuccessiveDA.IsTodaySigned(session.Group.ID, session.User.ID))
            {
                session.SendBack("你今天已经签过到啦！");
                return true;
            }

            if (SignInSuccessiveDA.TodaySignedGroupCount(session.User.ID) >= 3)
            {
                session.SendBack("每天最多只能在3个群组内签到！");
                return true;
            }

            Sign(session);
            return true;
        }

        protected abstract void Sign(InboxSession session);

        [ModuleEnter(Command = "今日签到内容", Description = "获取今日签到内容", ChannelType = ChannelType.Messaging)]
        public bool TodaySignContent(InboxSession session)
        {
            var content = GroupSignInDic.ContainsKey(session.Group.ID) ? GroupSignInDic[session.Group.ID].Content : "签到";
            session.SendBack($"今日签到内容是：{content}");
            return true;
        }
    }
}